﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 应用业务流程执行
    /// </summary>
    public class AppBizflowTaskService : PlatformAppService
    {
        private ActiviWorkflowManager _activiWorkflowManager;

        public AppBizflowTaskService(ActiviWorkflowManager activiWorkflowManager)
        {
            _activiWorkflowManager = activiWorkflowManager;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<AppBizflowTaskDto>> GetListAsync(WorkflowInstanceInput input)
        {
            string begntime = "";
            string endtime = "";

            if (!input.Begntime.HasValue)
            {
                begntime = DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                begntime = input.Begntime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }

            if (!input.Endtime.HasValue)
            {
                endtime = DateTime.Now.Date.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                endtime = input.Endtime.Value.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
            }

            var list = await _activiWorkflowManager.GetProcessInstance(begntime, endtime, input.Status);
            var itemsDto = ObjectMapper.Map<List<WorkflowInstance>, List<AppBizflowTaskDto>>(list);

            if (input.Pi == 0)
            {
                return new PagedResultDto<AppBizflowTaskDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = itemsDto.Count();
                var pagedItems = itemsDto.Skip((input.Pi - 1) * input.Ps).Take(input.Ps);

                return new PagedResultDto<AppBizflowTaskDto>(totalCount, pagedItems.ToList());
            }
        }

        /// <summary>
        /// 启动流程
        /// </summary>
        /// <returns></returns>
        public async Task PostAsync(AppBizflowTaskCreateDto input)
        {
            string userId = CurrentUser.Id == null ? "-" : CurrentUser.Id.Value.ToString("N");
            string bizFlowCode = input.BizFlowCode; //业务流程代码
            //var workflows = await _workflowAssocBizflowRepository.GetListAsync(w=>w.BizFlowCode == bizFlowCode);
            //var workflow = workflows.FirstOrDefault();
            string workflowId = "";
            var bizPars = input.BizPars;
            var assocUsers = input.AssocUsers;

            await _activiWorkflowManager.StartWorkflowInstance(workflowId, assocUsers, userId, bizPars);
        }

        /// <summary>
        /// 执行流程
        /// </summary>
        /// <returns></returns>
        public async Task PutAsync(AppBizflowTaskUpdateDto input)
        {
            string processInstanceId = input.ProcessInstanceId;
            string userId = CurrentUser.Id == null ? "-" : CurrentUser.Id.Value.ToString("N");
            await _activiWorkflowManager.ExecWorkflowInstance(processInstanceId, userId, input.BizPars);
        }
    }
}
